package com.wondream.myframework.io;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class InputStreamReaderTest {

    Log logger = LogFactory.getLog(InputStreamReaderTest.class);

    public static void main(String[] args) {
        InputStreamReader isr = null;
        try {
            File file = new File("./logs/oswData.txt");
            isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
            int len = 0;
            char[] data = new char[4];
            int ch = -1;
            StringBuilder sb = new StringBuilder();
//            while((len = isr.read(data, 0, 4))!=-1){
//            sb.append(data, 0, len);
            while((ch = isr.read())!=-1){
                System.out.println((char)ch);
            }
//            logger.info(sb.toString());
        } catch (Exception e){

        } finally {
            try {
                if(isr!=null){
                    isr.close();
                    isr = null;
                }
            } catch (Exception e){}
        }
    }


    public static void test2(){
        try {
            File file = ResourceUtils.getFile("classpath:application.properties");
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file));
            int bufferLen = 100;
            char[] buffer = new char[bufferLen];
            int readLen = 0;
            StringBuilder stringBuilder = new StringBuilder();
            while((readLen = inputStreamReader.read(buffer))>0){
                stringBuilder.append(new String(buffer, 0, readLen));
            }
            System.out.println("file content is:"+stringBuilder);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }
}
